// 获取频道数据
import {
  getUserChannelsApi,
  getAllChannelsApi,
  addUserChannelsApi,
  delUserChannelsApi,
  getArticlesApi,
  dislikeArticleApi,
  reportsArticleApi,
} from '@/api/home'
import { setChannelsInfo, getChannelsInfo } from '@/utils/storage_channels'
import { hasToken } from '@/utils/storage_token'
export const getUserChannelAsync = () => {
  // 此模块的redux数据可以不做持久化缓存
  return async (dispatch) => {
    if (hasToken()) {
      // 1.如果用户登录了，那么直接调接口获取用户频道,存redux
      const {
        data: { channels },
      } = await getUserChannelsApi()
      dispatch(saveUserChannels(channels))
    } else {
      // 从缓存中读取用户频道数据
      const channelsInfo = getChannelsInfo()
      if (channelsInfo.length > 0) {
        // 2.如果用户没有登录，但是缓存中有数据，那么读缓存数据，存redux
        dispatch(saveUserChannels(channelsInfo))
      } else {
        // 3.如果用户没有登录，缓存中也没有数据，那么就读默认的用户频道数据，再往缓存中存一份，再存redux
        const {
          data: { channels },
        } = await getUserChannelsApi()
        // 存储到本地缓存，用户下次没登录时，可以从缓存中获取上次存的频道数据
        setChannelsInfo(channels)
        dispatch(saveUserChannels(channels))
      }
    }
  }
}
// 存储用户频道数据
export const saveUserChannels = (channels) => ({
  type: 'SAVE_USER_CHANNELS',
  payload: channels,
})

// 获取所有频道数据
export const getAllChannelAsync = () => {
  return async (dispatch) => {
    const {
      data: { channels },
    } = await getAllChannelsApi()
    dispatch(saveAllChannels(channels))
  }
}

/**
 * 保存所有的频道
 * @param {*} channels
 * @returns
 */
export const saveAllChannels = (channels) => {
  return {
    type: 'SAVE_ALL_CHANNELS',
    payload: channels,
  }
}

// 添加用户频道
export const addChannelAsync = (channel) => {
  return async (dispatch, getState) => {
    // getState()可以拿到所有模块的redux数据
    // 获取原来用户的频道数据
    const userChannels = getState().home.userChannels
    if (hasToken()) {
      // 登录状态，调接口进行更新
      await addUserChannelsApi({
        channels: [channel],
      })
    } else {
      // 未登录，更新缓存
      setChannelsInfo([...userChannels, channel])
    }
    // 更新redux
    dispatch(saveUserChannels([...userChannels, channel]))
  }
}

// 删除用户频道
export const delChannelAsync = (channel) => {
  return async (dispatch, getState) => {
    // getState()可以拿到所有模块的redux数据
    // 获取原来用户的频道数据
    const userChannels = getState().home.userChannels
    if (hasToken()) {
      // 登录状态，调接口进行删除
      await delUserChannelsApi(channel.id)
    } else {
      // 未登录，更新缓存
      setChannelsInfo(userChannels.filter((item) => item.id !== channel.id))
    }
    // 更新redux
    dispatch(
      saveUserChannels(userChannels.filter((item) => item.id !== channel.id))
    )
  }
}

// 获取文章列表数据
export const getArticleListAsync = (channel_id, timestamp) => {
  return async (dispatch) => {
    const { data } = await getArticlesApi({ channel_id, timestamp })
    dispatch(
      saveArticleList({
        channel_id,
        // 作为下一组文章的开始时间
        timestamp: data.pre_timestamp,
        list: data.results,
      })
    )
  }
}

// 保存文章数据
export const saveArticleList = (payload) => ({
  type: 'SAVE_ARTICLELIST',
  payload,
})

// 加载更多的文章
export const getMoreArticleListAsync = (channel_id, timestamp) => {
  return async (dispatch) => {
    const { data } = await getArticlesApi({ channel_id, timestamp })
    dispatch(
      saveMoreArticleList({
        channel_id,
        // 作为下一组文章的开始时间
        timestamp: data.pre_timestamp,
        list: data.results,
      })
    )
  }
}

// 保存更多文章数据
export const saveMoreArticleList = (payload) => ({
  type: 'SAVE_MORE_ARTICLELIST',
  payload,
})

// 对文章不感兴趣
export const dislikeArticleAsync = (article_id, channel_id) => {
  return async (dispatch, getState) => {
    const oldArticles = getState().home.articles
    await dislikeArticleApi(article_id)
    // 修改频道的文章列表
    const articles = oldArticles[channel_id].list.filter(
      (item) => item.art_id !== article_id
    )
    // 去redux删除该篇文章
    dispatch(
      saveArticleList({
        channel_id,
        // 作为下一组文章的开始时间
        timestamp: oldArticles[channel_id].timestamp,
        list: articles,
      })
    )
  }
}

// 举报文章
export const reportArticleAsync = (article_id, channel_id, type) => {
  return async (dispatch, getState) => {
    const oldArticles = getState().home.articles
    await reportsArticleApi(article_id, type)
    // 修改频道的文章列表
    const articles = oldArticles[channel_id].list.filter(
      (item) => item.art_id !== article_id
    )
    // 去redux删除该篇文章
    dispatch(
      saveArticleList({
        channel_id,
        // 作为下一组文章的开始时间
        timestamp: oldArticles[channel_id].timestamp,
        list: articles,
      })
    )
  }
}
